package com.liang.maogouyuyin.ui.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.fragment.app.Fragment
import com.liang.maogouyuyin.AppConst
import com.liang.maogouyuyin.databinding.FragmentTranslateBinding
import com.liang.maogouyuyin.databinding.ItemHomeMasterBinding
import com.liang.maogouyuyin.databinding.ItemHomePetBinding
import com.liang.maogouyuyin.utils.PlayerUtil

class TranslateFragment: Fragment() {

    private lateinit var binding: FragmentTranslateBinding

    private var isDog = false

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = FragmentTranslateBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

    }

    fun addMasterTranslateView(path: String) {
        val childBinding1 = ItemHomeMasterBinding.inflate(layoutInflater, binding.translateContainer, false)
        val childBinding2 = ItemHomeMasterBinding.inflate(layoutInflater, binding.translateContainer, false)
        val params = LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.WRAP_CONTENT
        )

        binding.translateContainer.addView(childBinding1.root, params)

        childBinding1.itemHomeMasterText.text = "主人说："
        childBinding1.itemHomeMasterPlayer.setOnClickListener {
            PlayerUtil.playAudio(path)
        }

        binding.translateContainer.addView(childBinding2.root, params)

        childBinding2.itemHomeMasterText.text = "翻译"
        childBinding2.itemHomeMasterPlayer.setOnClickListener {
            if (isDog)
                PlayerUtil.playAudio("asset:///sound/dog/instruction_index_3.mp3")
            else
                PlayerUtil.playAudio("asset:///sound/cat/instruction_index_3.mp3")
        }

        binding.scrollLayout.post {
            binding.scrollLayout.fullScroll(View.FOCUS_DOWN)
        }
    }

    fun addPetTranslateView(path: String) {
        val childBinding1 = ItemHomePetBinding.inflate(layoutInflater, binding.translateContainer, false)
        val childBinding2 = ItemHomePetBinding.inflate(layoutInflater, binding.translateContainer, false)
        val params = LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.WRAP_CONTENT
        )

        binding.translateContainer.addView(childBinding1.root, params)

        childBinding1.itemHomePetText.text = "宠物说："
        childBinding1.itemHomePetPlayer.setOnClickListener {
            PlayerUtil.playAudio(path)
        }

        binding.translateContainer.addView(childBinding2.root, params)

        childBinding2.apply {
            itemHomePetText.text = "翻译"
            itemHomePetPlayer.visibility = View.GONE
            itemHomePetWave.visibility = View.GONE
            val position = (0..11).random()
            itemHomePetTranslation.text = AppConst.petTranslateText[position]
            itemHomePetTranslation.visibility = View.VISIBLE
        }

        binding.scrollLayout.post {
            binding.scrollLayout.fullScroll(View.FOCUS_DOWN)
        }
    }

    fun setDog(isDog: Boolean) {
        this.isDog = isDog
    }
}